package com.miyake.loveviewweb.client.viewer;

import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.client.ui.Composite;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.plugin.InstalledPluginListCombo;
import com.miyake.loveviewweb.shared.PluginType;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.ValueModel;

public class PluginDialogFactory {
    private static PluginDialogFactory instance;

    private PluginDialogFactory() {
        
    }
    
    public static PluginDialogFactory getInstance() {
        if (instance == null) {
            instance = new PluginDialogFactory();
        }
        return instance;
    }
    
    public interface ParserHandler {
        public void onSelect(Key pluginKey);
    }
    
    public interface AnalyzerHandler {
        public void onSelect(Key pluginKey, List<ValueModel> values, Map<String, String> data);
    }
        
    public interface ChartHandler {
        public void onSelect(Key pluginKey, Map<String, String> column);
    }
    
    public void createAnalyzer(final AnalyzerHandler handler, DataModel dataModel) {
        InstalledPluginListCombo pluginList = new InstalledPluginListCombo(PluginType.ANALYZER, dataModel);
        new BasicDialog(pluginList) {
            @Override
            protected void onOK(Composite composite) {
                InstalledPluginListCombo c = (InstalledPluginListCombo)composite;
                Key pluginKey = c.getSelectedItem().getKey();
                if (handler != null) {
                    Map<String, String> map = c.getData();
                    handler.onSelect(pluginKey, c.getValues(), c.getData());
                }
            }
        }.showDialog();
    }
    
    public void createFileParser(final ParserHandler handler, DataModel dataModel) {
        new BasicDialog(new InstalledPluginListCombo("FileParser", dataModel)) {
            @Override
            protected void onOK(Composite composite) {
                InstalledPluginListCombo c = (InstalledPluginListCombo)composite;
                Key pluginKey = c.getSelectedItem().getKey();
                if (handler != null) {
                    handler.onSelect(pluginKey);
                }
            }
        }.showDialog();
    }

    public void createChart(final ChartHandler handler, DataModel dataModel) {
        new BasicDialog(new InstalledPluginListCombo("Chart", dataModel)) {
            @Override
            protected void onOK(Composite composite) {
                InstalledPluginListCombo c = (InstalledPluginListCombo)composite;
                Key pluginKey = c.getSelectedItem().getKey();
                if (handler != null) {
                    handler.onSelect(pluginKey, c.getData());
                }
            }
        }.showDialog();
    }
}
